package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.GeneratePageService;
import com.heima.common.constants.article.ArticleConstants;
import com.heima.common.exception.CustException;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private AuthorMapper authorMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    GeneratePageService generatePageService;

    @Autowired
    ApArticleMapper apArticleMapper;
    @Value("${file.oss.web-site}")
    String webSite;
    @Value("${file.minio.readPath}")
    private String readPath;

    @Override
    public ResponseResult saveArticle(ArticleDto articleDto) {
        // 1. 检验参数
        if (articleDto == null || articleDto.getWmUserId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "saveArticle参数错误");
        }

        // 2. 封装对象
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(articleDto, apArticle);
        // 3. 完善信息
        ApAuthor author = authorMapper.selectOne(Wrappers.<ApAuthor>lambdaQuery()
                .eq(ApAuthor::getWmUserId, articleDto.getWmUserId()));
        if (author == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        apArticle.setAuthorId(Long.valueOf(author.getId()));
        apArticle.setAuthorName(author.getName());

        if (apArticle.getId() == null) {
            // 新建
            apArticle.setComment(0);
            apArticle.setViews(0);
            apArticle.setCollection(0);
            apArticle.setLikes(0);
            save(apArticle);
        } else {
            apArticleContentMapper.delete(Wrappers.<ApArticleContent>lambdaQuery()
                    .eq(ApArticleContent::getArticleId, apArticle.getId()));
            apArticleConfigMapper.delete(Wrappers.<ApArticleConfig>lambdaQuery()
                    .eq(ApArticleConfig::getArticleId, apArticle.getId()));
            updateById(apArticle);
        }
        // 添加config信息
        ApArticleConfig config = new ApArticleConfig();
        config.setArticleId(apArticle.getId());
        config.setIsComment(true);
        config.setIsForward(true);
        config.setIsDown(false); // 上架
        config.setIsDelete(false); // 正常
        apArticleConfigMapper.insert(config);

        // 添加content信息
        ApArticleContent content = new ApArticleContent();
        content.setArticleId(apArticle.getId());
        content.setContent(articleDto.getContent());
        apArticleContentMapper.insert(content);

        // 生成静态页面
        generatePageService.generateArticlePage(apArticle.getId());


        return ResponseResult.okResult(apArticle.getId());
    }

    @Override
    public ResponseResult load(Short loadType, ArticleHomeDto dto) {

        if (dto == null) {
            dto = new ArticleHomeDto();
        }
        Integer size = dto.getSize();
        if (size == null||size<=0) {
            size = 10;
        }
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        if (!loadType.equals(ArticleConstants.LOADTYPE_LOAD_NEW) && !loadType.equals(ArticleConstants.LOADTYPE_LOAD_MORE)) {
            loadType = ArticleConstants.LOADTYPE_LOAD_MORE;
        }

        List<ApArticle> apArticleList = apArticleMapper.loadArticleList(dto,loadType);

        if (apArticleList!=null&&apArticleList.size() >0){

            for (ApArticle apArticle : apArticleList) {
                apArticle.setStaticUrl(readPath+apArticle.getStaticUrl());
            }
        }


        ResponseResult responseResult = ResponseResult.okResult(apArticleList);
        responseResult.setHost(webSite);
        return responseResult;
    }
}
